<template>
	<view class="code-page">
		<view class="item" v-for="(item, index) in list" :key="index">
			<view class="top">
				<view class="text">
					<text>获得：{{ item.use_time }}</text>
				</view>
				<view class="text">
					<text>来源：{{ item.type == 1 ? '礼包订单' : '推三返一' }}</text>
				</view>
				<view class="text">
					<text>绑定订单: {{ item.order_sn }}</text>
					<text class="look">订单详情</text>
				</view>
			</view>
			<view class="bottom">
				<view class="label-box u-line-1">
					<text class="label">兑换码</text>
					<text class="value">{{ item.key_code }}</text>
				</view>
				<button class="btn" @click="copyCode(item.key_code, item.app_id)">复制去兑换</button>
			</view>
		</view>

		<u-loadmore :status="status" />
	</view>
</template>

<script>
	import { getKeyList } from '@/api/api'

	export default {
		data () {
			return {
				list: [],
				
				/* 分页 */
				status: 'loadmore',
				page: 0,
				last_page: 1,
			}
		},
		onLoad () {
			this.loadData()
		},
		onReachBottom () {
			this.getListData()
		},
		methods: {
			loadData () {
				this.page = 0
				this.last_page = 1
				this.list = []
				this.getListData()
			},
			async getListData () {
				if(this.page >= this.last_page) return
			
				this.status = 'loading'
				this.page = ++this.page
			
				const codeReq = await getKeyList({
					page: this.page,
					page_size: 10,
				})
				
				if (codeReq) {
					this.list = this.list.concat(codeReq.data.data)
					this.last_page = codeReq.data.last_page
					
					if (this.page >= this.last_page) {
						this.status = 'nomore'
					} else {
						this.status = 'loading'
					}
					
					return ''
				}
			},

			copyCode (code, appId) {
				uni.setClipboardData({
					data: code,
					success: () => {
						this.toMiniProgram(appId)
					}
				})
			},
			toMiniProgram (appId) {
				uni.navigateToMiniProgram({
					appId: appId,
					success: (res) => {
						console.log(res)
					},
					fail: (err) => {
						uni.showToast({
							title: '跳转失败',
							icon: 'none'
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss">
.code-page {
	box-sizing: border-box;
	padding: 18rpx 30rpx;
	min-height: 100%;
	background-color: $bg-color;
	.item {
		padding: 0 44rpx;
		border-radius: 14rpx;
		background-color: #fff;
		&:not(:last-child) {
			margin-bottom: 20rpx;
		}
		.top {
			padding: 30rpx 0;
			.text {
				color: #333;
				font-size: 28rpx;
				&:not(:last-child) {
					margin-bottom: 10rpx;
				}
				.look {
					margin-left: 12rpx;
					font-weight: bold;
					text-decoration: underline;
				}
			}
		}
		.bottom {
			padding: 30rpx 0;
			border-top: 1rpx solid #eee;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.label-box {
				flex: 1;
				color: #333;
				.label {
					font-size: 26rpx;
				}
				.value {
					margin-left: 28rpx;
					font-size: 36rpx;
					font-weight: bold;
				}
			}
			.btn {
				box-sizing: content-box;
				margin: 0;
				padding: 0;
				width: 182rpx;
				height: 84rpx;
				border-radius: 14rpx;
				border: 2rpx solid #5749f7;
				color: #5749f7;
				font-size: 30rpx;
				font-weight: bold;
				line-height: 84rpx;
				background-color: #e4e1ff;
			}
		}
	}
}
</style>
